﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security;
using System.Text;
using System.Web;

namespace Cosoware.Web.Themes
{
    /// <summary>
    /// Theme list helper watchs the list of themes in App_Themes folder.
    /// </summary>
    internal class ThemeListHelper
    {
        /// <summary>
        /// The themes' directory watcher.
        /// </summary>
        private static FileSystemWatcher _watcher;

        /// <summary>
        /// Determines if watcher will be used tu refresh list of themes.
        /// </summary>
        private static bool _watcherEnabled = true;

        /// <summary>
        /// The path to the current App_Themes folder.
        /// </summary>
        private static string _appThemesPath;

        /// <summary>
        /// The enumeration of themes;
        /// </summary>
        private static IEnumerable<string> _themes;

        /// <summary>
        /// Gets or sets a value indicating whether watcher enabled.
        /// </summary>
        /// <value><c>true</c> if watcher enabled; otherwise, <c>false</c>.</value>
        public static bool WatcherEnabled
        {
            get { return _watcherEnabled; }
            set { _watcherEnabled = value; }
        }

        /// <summary>
        /// Gets the list of an installed themes.
        /// </summary>
        /// <value>The list of an installed themes.</value>
        public static IEnumerable<string> Themes
        {
            get
            {
                if (_themes == null)
                    InitializeThemesList();
                return _themes;
            }
        }

        /// <summary>
        /// Initializes the themes list.
        /// </summary>
        private static void InitializeThemesList()
        {
            var path = AppThemesPath;
            if (_watcherEnabled && (_watcher == null))
                InitializeWatcher(path);
            _themes = GetSubdirectoriesList(path);
        }

        /// <summary>
        /// Themeses the path.
        /// </summary>
        private static string AppThemesPath
        {
            get
            {
                if (_appThemesPath == null)
                {
                    try
                    {
                        _appThemesPath = HttpContext.Current.Server.MapPath("~/App_Themes");
                    }
                    catch (NullReferenceException e)
                    {
                        throw new InvalidOperationException("HttpContext is invalid.", e);
                    }
                }
                return _appThemesPath;
            }
        }

        /// <summary>
        /// Initializes the system file watcher.
        /// </summary>
        /// <param name="path">The path.</param>
        private static void InitializeWatcher(string path)
        {
            _watcher = new FileSystemWatcher(path);
            _watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.LastWrite | NotifyFilters.Attributes;
            _watcher.Created += (sender, e) => _themes = null;
            _watcher.Deleted += (sender, e) => _themes = null;
            _watcher.Renamed += (sender, e) => _themes = null;
        }

        /// <summary>
        /// Gets the subdirectories list.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        private static IEnumerable<string> GetSubdirectoriesList(string path)
        {
            var subdirs = new DirectoryInfo(path).GetDirectories("*.*", SearchOption.TopDirectoryOnly);
            foreach (var subdir in subdirs)
            {
                if (subdir.Attributes == FileAttributes.Directory)
                    yield return subdir.Name;
            }
        }
    }
}
